package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.config.SceurityConfig;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.SCompany;
import com.tmeone.erpone.entity.SUser;
import com.tmeone.erpone.service.ISUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-16
 */
@RestController
public class SUserController {

    @Autowired
    private ISUserService userService;

    private String id;

    @ResponseBody
    @RequestMapping("/users")
    public IPage<SUser> selectUser(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current) {
        try {
            IPage<SUser> arg = new Page<>(current, 5);
            IPage<SUser> page = userService.pageUser(arg);
            return page;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/userRole/{id}")
    public void userRoleData(@PathVariable String id) {
        this.id = id;
    }

    @ResponseBody
    @RequestMapping("/userur")
    public SUser userSelect() {
        SUser sUser = userService.selectById(id);
        return sUser;
    }


    @RequestMapping(value = "/roleuserUpdate",method = RequestMethod.PUT)
    public void userUpdate(@RequestBody SUser sUser){
        userService.updateById(sUser);
        System.out.println("输出拿到的用户修改值："+sUser.toString());
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public void userDelete(@PathVariable String id){
        userService.removeById(id);
        System.out.println("要删除的用户对象："+id);
    }


    @GetMapping("/usersname")
    public JsonResult selectUserName(){
        List<SUser> list = userService.list();
        return new JsonResult(1,list);
    }

    @ResponseBody
    @RequestMapping("/comuser/userName1/{name}")
    public String userListName(@PathVariable String name) {
        List<SUser> list = userService.list();
        String str = "";
        for (SUser sUser : list) {
            if (sUser.getUserName().equals(name)) {
                str = "该账户不可用,请重新输入！";
                return str;
            }
        }
        str = "正常";
        return str;
    }

    @RequestMapping(value = "/comuser/user",method = RequestMethod.POST)
    public void userRegtister(@RequestBody SUser user){
        SceurityConfig sceurityConfig = new SceurityConfig();
        String s = sceurityConfig.encryptPassword(user.getPassWord());
        user.setPassWord(s);
        userService.save(user);
        System.out.println("测试密码加密："+s);
        System.out.println("用户注册测试："+user.toString());

    }
}
